/**
 * @Title: Controller.java
 * @Package controller
 * @author zhanglinlu
 * @date 2019年4月23日 下午2:33:43
 */
package com.zll.ccp.controller;

import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zll.ccp.model.User;
import com.zll.ccp.service.ILoginService;

/**
 * @ClassName: Controller
 * @Description: controller
 * @author zhanglinlu
 * @date 2019年4月23日 下午2:33:43
 * 
 */
@Controller
public class LoginController {
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private ILoginService loginService;

    /**
     * @Title: toLogin
     * @Description: 去登录页面
     * @return 登录页面
     */
    @RequestMapping("/loginAndRegist")
    public String toLogin() {
        return "login";
    }

    /**
     * @Title: login
     * @Description: 登录
     * @return 登录处理后跳转的页面
     */
    @RequestMapping("/login")
    @ResponseBody
    public Object login(@RequestBody User user, HttpSession session) {
        logger.info("登录中");
        JSONObject resulte = new JSONObject();
        String userName = user.getUserName();
        String password = user.getPassword();
        if (StringUtils.isBlank(userName)) {
            resulte.put("message", "用户名不能为空");
            resulte.put("success", false);
            return resulte;
        }
        // 登陆的主体
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
        try {
            subject.login(token);
            // 拿全部的信息
            User userAll = loginService.getUserByUserName(userName);
            session.setAttribute("userId", userAll.getId());
            session.setAttribute("admin", StringUtils.equals(userAll.getAdmin(), "admin"));// 是否管理员
            session.setAttribute("userName", userName);
            resulte.put("message", "登陆成功");
            resulte.put("success", true);
            return resulte;
        } catch (UnknownAccountException e) {
            // 不存在此账号抛出的异常
            resulte.put("message", "用户名或密码不正确");
            resulte.put("success", false);
            return resulte;
        } catch (IncorrectCredentialsException passe) {
            // 账号密码错误出的异常
            resulte.put("message", "用户名或密码不正确");
            resulte.put("success", false);
            return resulte;
        }
    }

    /**
     * @Title: regist
     * @Description: 注册
     * @param user 用户
     * @return 注册结果
     */
    @RequestMapping("/regist")
    @ResponseBody
    public Object regist(@RequestBody User user) {
        JSONObject resulte = new JSONObject();
        // 验证用户名不能为空
        if (StringUtils.isBlank(user.getUserName())) {
            resulte.put("message", "用户名不为空");
            resulte.put("success", false);
            return resulte;
        }
        if (loginService.existByUserName(user.getUserName())) {
            resulte.put("message", "该用户名已存在！");
            resulte.put("success", false);
            return resulte;
        }
        user.setId(StringUtils.replace(UUID.randomUUID().toString(), "-", ""));
        loginService.addUser(user);
        resulte.put("message", "注册成功！请登录");
        resulte.put("success", true);
        return resulte;
    }

}
